package com.iothub.netty4.nettypack;


import io.netty.buffer.Unpooled;
import io.netty.channel.ChannelHandlerContext;
import io.netty.util.CharsetUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.Date;

public class SendHandler implements Runnable {

    private static final Logger logger = LoggerFactory.getLogger(SendHandler.class);

    /*public static void SendMsg(ChannelHandlerContext ctx, EPortType portType, String msg, String aesKey)
            throws Exception {

        String aesMsg = "";
        if(portType == EPortType.TOOL_PORT){//与工具软件的通讯
            aesMsg = AesUtils.cbcEncrypt(msg,aesKey);
            ctx.channel().writeAndFlush(Unpooled.copiedBuffer(aesMsg, CharsetUtil.UTF_8));
        }else{//与设备的通讯
            byte[] bytes = AesUtils.ecbEncryptDevComm(msg,aesKey);
            ctx.channel().writeAndFlush(Unpooled.copiedBuffer(bytes));
        }
    }*/

    /**
     * 设置发送指令
     * @param clientState
     * @param cmd 需要发送的命令
     * @param state 命令对应的状态
     * @return
     */
    /*public static String setSendCmd(ClientState clientState, String cmd, ECmdAskState state){
        clientState.setCommStatus(state);
        clientState.setSendCmd(cmd);
        return clientState.getSendCmd();
    }*/

    @Override
    public void run() {
        logger.info("发送线程已启动！");
        Date dt = new Date();
        /*while(true){

            long sec = Convert.differenceCalculation(dt,new Date());
            if(sec >= 5){
                dt = new Date();
                logger.debug("发送线程在线");
            }

            try {
                if(NodeCtlBusiness.pushSendCmd2Queue() > 0)
                    Thread.sleep(5);
                else
                    Thread.sleep(20);
            } catch (InterruptedException e) {
                logger.error("发送线程sleep异常：" + e.getMessage());
            }

            //SendAtCmd();
            //SendHexCmd();
        }*/
    }

    /*private void SendAtCmd() {
        try{
            int size = NodeCtlBusiness.sendQueue.GetQueueCount();
            if(size == 0) return;

            for(int index=0;index<size;index++) {
                ClientState clientState = NodeCtlBusiness.sendQueue.OutQueue();
                clientState.sendMsg();
                //需要判断是否还有数据要发送
                if(!clientState.getSendBuf().cmdsIsNull()) NodeCtlBusiness.sendQueue.InQueue(clientState);
            }
        }catch(Exception e){
            logger.error("发送线程发送异常：" + e.getMessage());
        }
    }*/

    /*private void SendHexCmd() {

        try{
            Set<String> keySet = HexProtocolEncoder.mpbs.keySet();
            if(keySet.size() == 0) return;
            ClientState[] clientStates = (ClientState[]) RemoteNodeController.getCtxMap().values().toArray();
            for(String uid : keySet){
                byte[] bytes = HexProtocolEncoder.encoder(uid);
                if(bytes == null || bytes.length < ConstantText.frameMinLength) continue;
                SysDeviceInfo info = GlobalFunction.getDeviceInfoByDeviceId(uid);

                List<ClientState> csc = Arrays.stream(clientStates).filter(o->
                        o.getDeviceMac().equals(uid)).collect(Collectors.toList());
                if(csc == null || csc.size() == 0) continue;
                csc.get(0).sendMsg(bytes);
            }
        }catch (Exception e){
            logger.error("十六进制信息发送异常，异常信息：",e.getMessage());
        }
    }*/

}
